package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.GetKlineDataVo;
import com.ruoyi.system.domain.vo.KlineAddVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Kline;
import com.ruoyi.system.service.IKlineService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * K线图Controller
 * 
 * @author ruoyi
 * @date 2024-03-19
 */
@RestController
@RequestMapping("/system/kline")
public class KlineController extends BaseController
{
    @Autowired
    private IKlineService klineService;

    /**
     * 查询K线图列表
     */
    @PreAuthorize("@ss.hasPermi('system:kline:list')")
    @GetMapping("/list")
    public TableDataInfo list(Kline kline)
    {
        Long userId = SecurityUtils.getUserId();
        if (SecurityUtils.isAdmin(userId)) {
            kline.setUserId(userId);
        }
        startPage();
        List<Kline> list = klineService.selectKlineList(kline);
        return getDataTable(list);
    }

    /**
     * 导出K线图列表
     */
    @PreAuthorize("@ss.hasPermi('system:kline:export')")
    @Log(title = "K线图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Kline kline)
    {
        List<Kline> list = klineService.selectKlineList(kline);
        ExcelUtil<Kline> util = new ExcelUtil<Kline>(Kline.class);
        util.exportExcel(response, list, "K线图数据");
    }

    /**
     * 获取K线图详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:kline:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(klineService.selectKlineById(id));
    }

    /**
     * 从Python获取数据
     */
    @PostMapping(value = "/getKlineData")
    public AjaxResult getKlineData(@RequestBody GetKlineDataVo getKlineDataVo)
    {
        klineService.getKlineData(getKlineDataVo);
        return success();
    }


    /**
     * 新增K线图
     */
    @PreAuthorize("@ss.hasPermi('system:kline:add')")
    @Log(title = "K线图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KlineAddVo kline)
    {
        return toAjax(klineService.insertKline(kline));
    }

    /**
     * 修改K线图
     */
    @PreAuthorize("@ss.hasPermi('system:kline:edit')")
    @Log(title = "K线图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Kline kline)
    {
        return toAjax(klineService.updateKline(kline));
    }

    /**
     * 删除K线图
     */
    @PreAuthorize("@ss.hasPermi('system:kline:remove')")
    @Log(title = "K线图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(klineService.deleteKlineByIds(ids));
    }
}
